package com.cpi.cornpp.instructions.futures.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.instructions.futures.dto.InstructionsFuturesDTO;
import com.cpi.cornpp.instructions.futures.exception.InstructionsFuturesException;
import com.cpi.cornpp.instructions.futures.view.InstructionsFuturesView;
import com.cpi.cornpp.period.view.PeriodView;
import com.cs.common.utils.NumberUtil;

/**
 * Purchases Instructions Futures Converter
 * @author Edgar Joao
 *
 */
public class InstructionsFuturesConverter {
	/** Precio Menor SI */
	private static final String YES = "Y";
	/** Precio Menor NO */
	private static final String NO = "N";

	/**
	 * Purchases Instructions Converter DTO to View
	 * 
	 * @author Edgar Joao
	 * @param dto
	 * @param list2 
	 * @param list 
	 * @return
	 * @throws InstructionsFuturesException
	 */
	public InstructionsFuturesView converterInstructionsFuturesDtoToView(InstructionsFuturesDTO dto, List accountPeriods, List applicatonPeriods)throws InstructionsFuturesException{
		InstructionsFuturesView view = null;
		PeriodView aux1 = null;
		PeriodView aux2 = null;
		try{
			if(dto != null){
				view = new InstructionsFuturesView();
				view.setPurchaseInstructionFutureId(dto.getPurchaseInstructionFutureId());
				view.setPurchaseInstructionDetailId(dto.getPurchaseInstructionDetailId());
				view.setApplicationFuturePeriodId(dto.getApplicationFuturePeriodId());
				view.setAccountFuturePeriodId(dto.getAccountFuturePeriodId());
				view.setQuantity(dto.getQuantity());
				view.setPrice(NumberUtil.convertPrice(dto.getPrice()));
				view.setMiniorPrice(dto.getMiniorPrice() == 1 ? YES : NO);
				view.setStopLoss(NumberUtil.convertPrice(dto.getStopLoss()));
				view.setAccountPeriod(accountPeriods);
				view.setApplicationPeriod(applicatonPeriods);
				view.setBalance(NumberUtil.convertInteger(dto.getQuantity()-dto.getBalance()));
				
				aux1 = new PeriodView(view.getAccountFuturePeriodId());
				if(accountPeriods.contains(aux1)){
					aux1 = (PeriodView)accountPeriods.get(accountPeriods.indexOf(aux1));
					aux1.setSelected(true);
					view.setAccountFuturePeriodDescription(aux1.getPeriodName());
				}				
				
				aux2 = new PeriodView(view.getApplicationFuturePeriodId());
				if(applicatonPeriods.contains(aux2)){
					aux2 = (PeriodView)applicatonPeriods.get(applicatonPeriods.indexOf(aux2));
					aux2.setSelected(true);
					view.setApplicationFuturePeriodDescription(aux2.getPeriodName());
				}				

				
				
			}
		}catch( Exception exception ){
			InstructionsFuturesException instruPurchasesInstructionsFuturesException=null;
			exception.printStackTrace(System.out);
			instruPurchasesInstructionsFuturesException= new InstructionsFuturesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instruPurchasesInstructionsFuturesException;
		}
		return view;
	}
	
	
	/**
	 * Purchases Instructions Converter DTO to View
	 * 
	 * @author Edgar Joao
	 * @param dto
	 * @param list2 
	 * @param list 
	 * @return
	 * @throws InstructionsFuturesException
	 */
	public InstructionsFuturesView converterInstructionsFuturesDtoToView(InstructionsFuturesDTO dto)throws InstructionsFuturesException{
		InstructionsFuturesView view = null;
		try{
			if(dto != null){
				view = new InstructionsFuturesView();
				view.setPurchaseInstructionFutureId(dto.getPurchaseInstructionFutureId());
				view.setPurchaseInstructionDetailId(dto.getPurchaseInstructionDetailId());
				view.setApplicationFuturePeriodId(dto.getApplicationFuturePeriodId());
				view.setAccountFuturePeriodId(dto.getAccountFuturePeriodId());
				view.setQuantity(dto.getQuantity());
				view.setPrice(NumberUtil.convertQuantity(dto.getPrice()));
				view.setMiniorPrice(dto.getMiniorPrice() == 1 ? YES : NO);
				view.setStopLoss(NumberUtil.convertQuantity(dto.getStopLoss()));
				
			}
		}catch( Exception exception ){
			InstructionsFuturesException instruPurchasesInstructionsFuturesException=null;
			exception.printStackTrace(System.out);
			instruPurchasesInstructionsFuturesException= new InstructionsFuturesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instruPurchasesInstructionsFuturesException;
		}
		return view;
	}
	
	/**
	 * Purchases Instructions Converter View to DTO 
	 * 
	 * @author Edgar Joao
	 * @param view
	 * @return
	 * @throws InstructionsFuturesException
	 */
	public InstructionsFuturesDTO converterInstructionsFuturesViewToDto(InstructionsFuturesView view) throws InstructionsFuturesException{
		InstructionsFuturesDTO dto = null;
		try{
			if(view != null){
				dto = new InstructionsFuturesDTO();
				dto.setPurchaseInstructionFutureId(view.getPurchaseInstructionFutureId());
				dto.setPurchaseInstructionDetailId(view.getPurchaseInstructionDetailId());
				dto.setAccountFuturePeriodId(view.getAccountFuturePeriodId());
				dto.setApplicationFuturePeriodId(view.getApplicationFuturePeriodId());
				dto.setQuantity(view.getQuantity());
				dto.setPrice(NumberUtil.parseDouble(view.getPrice()));
				dto.setMiniorPrice(StringUtils.equals(view.getMiniorPrice(), YES) ? 1 : 0);
				dto.setStopLoss(NumberUtil.parseDouble(view.getStopLoss()));
			}			
		}catch( Exception exception ){
			InstructionsFuturesException instruPurchasesInstructionsFuturesException=null;
			exception.printStackTrace(System.out);
			instruPurchasesInstructionsFuturesException= new InstructionsFuturesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instruPurchasesInstructionsFuturesException;
		}
		return dto;
	}
	/**
	 * 
	 * @author Edgar Joao
	 * @param instructionsFuturesDTOs
	 * @param list 
	 * @return
	 * @throws InstructionsFuturesException
	 */
	public List convertDTOsToViews(List instructionsFuturesDTOs, List accountPeriods, List applicatonPeriods) throws InstructionsFuturesException{
		List instructionsFuturesDTOList = null;
		Iterator iterator = null;
		InstructionsFuturesDTO instructionsFuturesDTO = null;
		InstructionsFuturesView instructionsFuturesView = null;

		try{
			instructionsFuturesDTOList = new ArrayList(0);
			iterator = instructionsFuturesDTOs.iterator();
			while(iterator.hasNext()){
				instructionsFuturesDTO = (InstructionsFuturesDTO) iterator.next();
				instructionsFuturesView = converterInstructionsFuturesDtoToView(instructionsFuturesDTO);
				

				
				instructionsFuturesView.setApplicationPeriod(applicatonPeriods);
				instructionsFuturesDTOList.add(instructionsFuturesView);			
			}
		}catch( Exception exception ){
			InstructionsFuturesException instruPurchasesInstructionsFuturesException=null;
			exception.printStackTrace(System.out);
			instruPurchasesInstructionsFuturesException= new InstructionsFuturesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instruPurchasesInstructionsFuturesException;
		}		
		return instructionsFuturesDTOList;
	}
	/**
	 * @author Edgar Joao
	 * @param instructionsFuturesViews
	 * @return
	 * @throws InstructionsFuturesException
	 */
	public List convertViewsToDTOs(List instructionsFuturesViews) throws InstructionsFuturesException{
		List instructionsFuturesViewsList = null;
		Iterator iterator = null;
		InstructionsFuturesDTO instructionsFuturesDTO = null;
		InstructionsFuturesView instructionsFuturesView = null;
		try{
			instructionsFuturesViewsList = new ArrayList(0);
			iterator = instructionsFuturesViews.iterator();
			while(iterator.hasNext()){
				instructionsFuturesView = (InstructionsFuturesView) iterator.next();
				instructionsFuturesDTO = converterInstructionsFuturesViewToDto(instructionsFuturesView);
				instructionsFuturesViewsList.add(instructionsFuturesDTO);
			}			
		}catch( Exception exception ){
			InstructionsFuturesException instruPurchasesInstructionsFuturesException=null;
			exception.printStackTrace(System.out);
			instruPurchasesInstructionsFuturesException= new InstructionsFuturesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instruPurchasesInstructionsFuturesException;
		}
		return instructionsFuturesViewsList;
	}
}
